package com.yc.ui;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ibm.icu.text.DateFormat;
import com.yc.Idao.LoginIDao;
import com.yc.dao.LoginDao;
import com.yc.ui.dialog.LiuyanDialog;
import com.yc.ui.dialog.MoneyDialog;
import com.yc.ui.dialog.Pay_upDialog;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;

public class Check_out extends Composite {
	private Text text;
	private static Text text_1;
	private static Text text_2;
	private static Text text_3;
	private static Text text_4;
	private Text text_5;
	private static Text text_6;
	private static Text text_7;
	private Table table;

	private static double price = 0;
	private static double s_price = 0;

	private static LoginIDao dao = new LoginDao();

	private String l_id;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Check_out(Composite parent, int style, String l_id) {
		super(parent, style);
		this.l_id = l_id;
		setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundImage( SWTResourceManager.getImage(Check_out.class, "/com/yc/images/退房结算界面主界面2.PNG") );
		SWTUtil.imagetSize(composite);
		
		Map<String, Object> map = MainUI.map;

		Label label = new Label(composite, SWT.NONE);
		label.setBounds(67, 71, 76, 20);
		label.setText("姓名：");

		text = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text.setBounds(149, 71, 142, 26);
		text.setText(map.get("c_name").toString());

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBounds(67, 125, 76, 20);
		lblNewLabel.setText("房间号：");

		text_1 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_1.setBounds(149, 122, 142, 26);

		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setBounds(67, 184, 76, 20);
		lblNewLabel_1.setText("身份证号");

		text_2 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_2.setBounds(149, 181, 142, 26);

		Label lblNewLabel_2 = new Label(composite, SWT.NONE);
		lblNewLabel_2.setBounds(67, 239, 76, 20);
		lblNewLabel_2.setText("退还金额");

		text_3 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_3.setBounds(149, 233, 142, 26);

		Label lblNewLabel_3 = new Label(composite, SWT.NONE);
		lblNewLabel_3.setBounds(724, 125, 76, 20);
		lblNewLabel_3.setText("退房日期：");

		Label lblNewLabel_3_1 = new Label(composite, SWT.NONE);
		lblNewLabel_3_1.setText("入住日期：");
		lblNewLabel_3_1.setBounds(724, 71, 76, 20);

		Date date = new Date(System.currentTimeMillis()); // 当前日期
		String dateString = String.format("%tF", date);
		text_4 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_4.setBounds(806, 68, 142, 26);


		text_5 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_5.setBounds(806, 122, 142, 26);
		text_5.setText(dateString);

		Label lblNewLabel_4 = new Label(composite, SWT.NONE);
		lblNewLabel_4.setBounds(724, 239, 76, 20);
		lblNewLabel_4.setText("应收金额");

		text_6 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_6.setBounds(806, 178, 142, 26);

		Label lblNewLabel_3_2 = new Label(composite, SWT.NONE);
		lblNewLabel_3_2.setText("入住天数");
		lblNewLabel_3_2.setBounds(724, 178, 76, 20);

		text_7 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_7.setBounds(806, 239, 142, 26);

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setBounds(547, 302, 98, 30);
		btnNewButton_1.setText("打印账单");

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		table.setBounds(67, 370, 896, 155);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(144);
		tblclmnNewColumn.setText("住宿编号");

		TableColumn tblclmnNewColumn_6 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_6.setWidth(115);
		tblclmnNewColumn_6.setText("房间号");

		TableColumn tblclmnNewColumn_5 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_5.setWidth(116);
		tblclmnNewColumn_5.setText("住宿日期");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(107);
		tblclmnNewColumn_1.setText("今日日期");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(121);
		tblclmnNewColumn_2.setText("住宿天数");

		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(131);
		tblclmnNewColumn_4.setText("消耗金额");

		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(128);
		tblclmnNewColumn_3.setText("押金余额");


		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Pay_upDialog pu = new Pay_upDialog(getShell(), SWT.NONE);
				pu.open();
			}
		});
		btnNewButton_3.setBounds(846, 543, 98, 30);
		btnNewButton_3.setText("补交房费");

		Borider(l_id);

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				LiuyanDialog ld = new LiuyanDialog(getShell(), SWT.NONE, text_1.getText().trim());
				ld.open();

				String c_cardid = text_2.getText().trim(); // 身份证
				String endDate = text_5.getText().trim(); // 退房时间
				String money = MainUI.money; // 账户余额
				if((s_price - price) == 0) {
					SWTUtil.showMessage(getShell(), "您还没有住满一天哦，请明天再尝试退房吧！");
					return ;
				}
				if(Double.parseDouble(money) < (s_price - price)) {
					StringUtil.showMessageBox(getShell(), "您的余额不足以支付房费，为您跳转至充值界面\n(您至少需要再充值" +  (s_price - price - Double.parseDouble(money)) + "才能支付房费)");
					MoneyDialog md = new MoneyDialog(getShell(), SWT.NONE, map.get("c_cardid").toString());
					md.open();
					return;
				}

				try {
					int re = 0;
					re = dao.UpdateKiln(endDate, c_cardid, s_price - price);
					if(re == 0) {
						StringUtil.showMessageBox(getShell(), "退房失败");
						return;
					}else {
						StringUtil.showMessageBox(getShell(), "谢谢惠顾");
						Room.Color(text_1.getText().trim());
					}
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}


			}
		});
		btnNewButton.setBounds(322, 302, 98, 30);
		btnNewButton.setText("结账");


	}

	public void Borider(String l_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtil.checkNull(l_id)) {
			String c_cardid;
			c_cardid = MainUI.map.get("c_cardid").toString();
			map = dao.SelectLogin(c_cardid);
		}else {
			map = dao.SelectEntry(Integer.parseInt(l_id));
		}

		if(map == null) {
			return ;
		}
		
		System.out.println(StringUtil.toString(map.get("r_id")));
		// 房间号
		text_1.setText(" " + StringUtil.toString(map.get("r_id")));

		// 身份证 
		text_2.setText(" " + map.get("c_cardid").toString());

		// 入住日期
		text_4.setText(" " + map.get("start_date").toString());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date star = sdf.parse(text_4.getText().trim());
			Date date = new Date(System.currentTimeMillis()); // 当前日期
			String dateString = String.format("%tF", date);
			Date enDate = sdf.parse(dateString);
			text_6.setText(" " + Timediff(star, enDate));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Map<String, Object> mapRoom = dao.SelectPrice(text_1.getText().trim());
		if(mapRoom != null) {
			price = Double.parseDouble(mapRoom.get("r_price").toString()) * 0.2;
			text_3.setText(price + "");

			s_price = Double.parseDouble(mapRoom.get("r_price").toString()) * Integer.parseInt(text_6.getText().trim());
			text_7.setText("" + s_price);
		}

	}

	/**
	 * 求两个日期之间的差
	 * @param star 开始时间
	 * @param endDay 结束时间
	 * @return 时间差
	 */
	public static int Timediff(Date star, Date enDay) {
		Date nextDay=star;
		int i = 0;
		while(nextDay.before(enDay)){//当明天不在结束时间之前是终止循环
			Calendar cld = Calendar.getInstance();
			cld.setTime(star);
			cld.add(Calendar.DATE, 1);
			star = cld.getTime();
			//获得下一天日期字符串
			nextDay = star; 
			i++;
		}
		return i;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
